<?php 
/*************************************************************************************
 * edittrip.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.12 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/17 08:41:58 $
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?PHP
include_once("secure.php");
/*
edite un point
*/
$file = urldecode($_GET["file"]);
$showtrip = urldecode($_GET["showtrip"]);
include_once("dbaccess.php");
$pointarray = getTripPoints($showtrip);
$opttripseqarray = getTripPointsOrder($showtrip);
$opttriparray = getTripOptionalData($showtrip);
$grouparray = getGroups();
if (!empty($opttripseqarray)) {
	// check that no place has been removed from the trip
	$changedTripOrder = false;
	foreach($opttripseqarray[$showtrip] as $seq => $pointname) {
		if (!array_key_exists($pointname["name"], $pointarray)) {
			unset($opttripseqarray[$showtrip][$seq]);
			$changedTripOrder = true;
		}
	}
	if ($changedTripOrder) {
		$tmpopttripseqarray = array();
		$i = 0;
		foreach($opttripseqarray[$showtrip] as $seq => $pointname) {
			$tmpopttripseqarray[$i] = $pointname;
			$i++;
		}
		$opttripseqarray[$showtrip] = $tmpopttripseqarray;
	}
}
?>
<html>
<head>
<link rel="stylesheet" href="gearth.css" type="text/css" media="screen" />
<script src="scriptaculous/prototype.js" type="text/javascript"></script>
<script src="scriptaculous/scriptaculous.js" type="text/javascript"></script>
<style>
body {
	background-color:white;
	font-size:x-small;
	padding:0;
}
li {
	cursor:move;
	       font-size:medium;
}
#buttons {
	padding:0;
	position:fixed;
	bottom:2px;
	right:2px;
	z-index:100;
}
#edittable {
   height:85%;
overflow:auto;
}
</style>
<script>
function checkValues(aform) {
// alert(Sortable.serialize('edittriplist'));
   aform.pointorder.value = escape(Sortable.serialize('edittriplist'));
}
function addPoint() {
//TODO 
// add li item with name
}
</script>
</head>
<body>
<?php
// load tag list
$tripName = "All";
if (!empty($showtrip)) {
	$tripName = $showtrip;
}
?>
<form method="post" action="doedittrip.php" name="formedittrip" onsubmit="return(checkValues(this));">

<div id="buttons">
<input type="button" name="docancel" value="<?php echo $lang_cancel_button; ?>" onclick="parent.callTrip('<?php echo $file; ?>', '<?php echo $showtrip; ?>')"> 
<input type="submit" name="doedit" value="<?php echo $lang_save_button; ?>"> 
</div>

<input type='hidden' name='showtrip' value='<?php echo $showtrip; ?>' >
<input type='hidden' name='langue' value='<?php echo $langue; ?>' >
<input type='hidden' name='file' value='<?php echo $file; ?>' >
<input type='hidden' name='pointorder' value='' >

<div id="edittable">
<table class="editbook">
<ol id="edittriplist">
<?php
/**************************
affichage du resultat du parsing
**************************/
if (!empty($showtrip) && $showtrip!= "Latest" && $showtrip!= "All" && $showtrip!="Visitor") {
	// all trip points
	$tag = $opttripseqarray[$showtrip];
	if (empty($tag)) {
		$tag = $pointarray;
	} else {
		$tagnames = $opttriparray[$showtrip];
		foreach($pointarray as $pointname => $point) {
			if (!isset($tagnames[$pointname])) {
				array_push($tag, $point);
			}
		}
		// remove old points in sorted list that are not in trip anymore
		foreach($opttripseqarray[$showtrip] as $seq => $pointname) {
			if (!array_key_exists($pointname["name"], $pointarray)) {
				unset($opttripseqarray[$showtrip][$seq]);
				unset($opttriparray[$pointname["name"]]);
			}
		}
	}
	ksort($tag);
	printTrip($tag, $pointarray);
}
function printTrip($tag, $pointarray) {
	$i = 0;
	foreach($tag as $book) {
		printPoint($pointarray[$book["name"]]["title"], $book["lat"],$book["long"],$i++);
	}
}
function printPoint($destiName, $destiLat, $destiLng, $i) {
	echo '<li id="point_'.$i.'">'.$destiName.'</li>';
}
?>
</ol>
</table>
</div>
<?php
// load tag list
$grouparraySelect = "";
ksort($grouparray);
reset($grouparray);
$grouparraySelect .= "<option value=''>".$lang_select_group."</option>";
foreach ($grouparray as $groupname => $triparray) {
	$grouparraySelect .= "<option value='".$groupname."'>".$groupname."</option>";
}
$r = " <input type='text' name='tripgroups' value='". getTripGroups($showtrip) ."' size='20' >\n";
$r .= ' <select name="grouplist" onchange="x=this.options[this.selectedIndex].value; if (x != \'\') {sep= \'; \'; if (this.form.tripgroups.value==\'\') {sep=\'\';} this.form.tripgroups.value+=sep+x;}">'.$grouparraySelect.'</select>';
echo $r;
?>
</form>
<script>
	// create sortable 
	Sortable.create("edittriplist");
</script>


</body>
</html>
